<?php

namespace addons\yungift\model;

use addons\yungift\library\ModelTrait;
use think\Model;

/**
 * 成员日志模型
 */
class MemberLog extends Model
{
    use ModelTrait;

    // 表名,不含前缀
    protected $name = 'yungift_member_log';

    protected $resultSetType = 'collection';

    // 成员数据
    public function memberData()
    {
        return $this->hasOne(Member::class, 'id', 'member_id');
    }

    // 操作
    const ACTION_NAME_CODE_LOGIN     = 1;
    const ACTION_NUMBER_CODE_LOGIN   = 2;
    const ACTION_PHONE_CAPTCHA_LOGIN = 3;
    const ACTION_WECHAT_PHONE_LOGIN  = 4;
    const ACTION_WECHAT_AUTO_LOGIN   = 5;
    const ACTION_LOGOUT              = 6;
    const ACTION_UPDATE_INFO         = 11;
    const ACTION_CHANGE_CODE         = 12;
    const ACTION_CHANGE_PHONE        = 13;

    public static function getActionList()
    {
        return [
            self::ACTION_NAME_CODE_LOGIN     => __('用户登入-姓名登录码'),
            self::ACTION_NUMBER_CODE_LOGIN   => __('用户登入-编号登录码'),
            self::ACTION_PHONE_CAPTCHA_LOGIN => __('用户登入-手机验证码'),
            self::ACTION_WECHAT_PHONE_LOGIN  => __('用户登入-微信快捷验证'),
            self::ACTION_WECHAT_AUTO_LOGIN   => __('用户登入-微信自动'),
            self::ACTION_LOGOUT              => __('退出登录'),
            self::ACTION_UPDATE_INFO         => __('更新个人资料'),
            self::ACTION_CHANGE_CODE         => __('修改登录码'),
            self::ACTION_CHANGE_PHONE        => __('修改手机号'),
        ];
    }

    public function getActionTextAttr($value, $data)
    {
        $optionList = self::getActionList();

        return $optionList[$data['action']] ?? '';
    }

    public static function quickLog($action, $memberId, $data = null)
    {
        self::create([
            'action'     => $action,
            'member_id'  => $memberId,
            'data'       => $data ? json_encode($data) : null,
            'created_at' => time()
        ]);
    }
}
